package com.bootdo.system.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.ConfirmImgDao;
import com.bootdo.system.domain.ConfirmImgDO;
import com.bootdo.system.vo.DeliveryConfirmDetailVO;
import com.bootdo.system.vo.DeliveryDetailVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.bootdo.system.dao.ConfirmListDao;
import com.bootdo.system.domain.ConfirmListDO;
import com.bootdo.system.service.ConfirmListService;
import org.springframework.web.multipart.MultipartFile;


@Service
public class ConfirmListServiceImpl implements ConfirmListService {
	private static final Logger logger = LoggerFactory.getLogger(ConfirmListServiceImpl.class);

	@Autowired
	private ConfirmListDao confirmListDao;

	@Autowired
	private ConfirmImgDao confirmImgDao;
	
	@Override
	public ConfirmListDO get(String id){
		return confirmListDao.get(id);
	}
	
	@Override
	public List<DeliveryConfirmDetailVO> list(Map<String, Object> map){
		List<DeliveryConfirmDetailVO> DeliveryConfirmDetailVOS = confirmListDao.list(map);
		for (DeliveryConfirmDetailVO deliveryConfirmDetailVO:DeliveryConfirmDetailVOS) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if(deliveryConfirmDetailVO.getFreightDate() != null){
				deliveryConfirmDetailVO.setFreightTime(format.format(deliveryConfirmDetailVO.getFreightDate()));
			}

			//根据传递单号查回执单图片
			List<ConfirmImgDO> reportImgDOS = new ArrayList<>();
			reportImgDOS = confirmImgDao.getImgInfo(deliveryConfirmDetailVO.getExpressNo());
			if(!reportImgDOS.isEmpty()){
				deliveryConfirmDetailVO.setImgUrls(reportImgDOS);
			}
		}
		return DeliveryConfirmDetailVOS;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return confirmListDao.count(map);
	}
	
	@Override
	public int save(ConfirmListDO confirmList){
		return confirmListDao.save(confirmList);
	}
	
	@Override
	public int update(ConfirmListDO confirmList){
		return confirmListDao.update(confirmList);
	}
	
	@Override
	public int remove(String id){
		return confirmListDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return confirmListDao.batchRemove(ids);
	}

	@Override
	public void importInfo(MultipartFile file, Long userId) throws Exception {
		try {
			// 导入模板参数
			ImportParams importParams = new ImportParams();
			// easypoi 导入数据
			ExcelImportResult<ConfirmListDO> result =
					ExcelImportUtil.importExcelMore(file.getInputStream(), ConfirmListDO.class, importParams);
			// 获取物料数据
			List<ConfirmListDO> list = result.getList();
			if (CollectionUtils.isNotEmpty(list)) {
				//查询当天的数据导入次数
				Integer importNum = confirmListDao.getImportNum();
				if(null == importNum){
					importNum = 1;
				}else{
					importNum = importNum + 1;
				}

				for (ConfirmListDO listDO : list) {
					if(StringUtils.isBlank(listDO.getProductName())){
						continue;
					}
					//查询是否已经导入过
					String existId = confirmListDao.getInfoByProNameAndGuige(listDO);
					if (StringUtils.isNotBlank(existId)) {
						listDO.setId(existId);
						confirmListDao.update(listDO);
					} else {
						String id = UUID.randomUUID().toString().replaceAll("-", "");
						listDO.setId(id);
						listDO.setIsDelete(0);
						listDO.setCreateUser(ShiroUtils.getUserId().toString());
						listDO.setPrintNum(0);
						listDO.setImportNum(importNum);
						confirmListDao.save(listDO);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("导入模板有误", e.getStackTrace());
			throw new Exception("导入模板有误", e);
		}
	}

	@Override
	public DeliveryConfirmDetailVO detail(String id) {
		DeliveryConfirmDetailVO deliveryConfirmDetailVO = confirmListDao.detail(id);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		deliveryConfirmDetailVO.setFreightTime(format.format(deliveryConfirmDetailVO.getFreightDate()));
		return deliveryConfirmDetailVO;
	}

	@Override
	public void updatePrintNum(String[] ids) {
		for(String id : ids){
			ConfirmListDO listDO = confirmListDao.get(id);
			listDO.setPrintNum(listDO.getPrintNum() + 1);
			confirmListDao.update(listDO);
		}
	}
	
}
